package com.learning.springboot.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.learning.springboot.RegistrationServletFilterListener.TestFilter;
import com.learning.springboot.RegistrationServletFilterListener.TestListener;
import com.learning.springboot.RegistrationServletFilterListener.TestServlet;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


/**
 * Created by td on 2017/9/13.
 */
@Configuration//@Configuration相当于springMvc中的xml配置文件，@bean相当于springMvc的beans
public class AppConfiguration {

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        //创建转换对象
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //创建配置文件对象
        FastJsonConfig config = new FastJsonConfig();
       // config.setSerializeFilters(SerializerFeature.BeanToArray);
        converter.setFastJsonConfig(config);
        return new HttpMessageConverters(converter);
    }
    @Bean
    public ServletRegistrationBean testServletRegistration(){
        System.out.println("我运行了.....");
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new TestServlet());
        registrationBean.addUrlMappings("/abc");
        return registrationBean;

    }

    @Bean
    public FilterRegistrationBean testFilterRegistration(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new TestFilter());
        registrationBean.addUrlPatterns("/");
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean testServletListenerRegistration(){
        ServletListenerRegistrationBean<TestListener> registrationBean = new ServletListenerRegistrationBean<>(new TestListener());
        return registrationBean;
    }

}
